package com.quanyan.place.web.api;

import com.quanyan.api.APIResponse;
import com.quanyan.api.enums.ApiResponseEnum;
import com.quanyan.place.client.BizPlaceClient;
import com.quanyan.place.client.PlaceClient;
import com.quanyan.place.constants.PlaceConstants;
import com.quanyan.place.entity.apireq.ReqOrderOperate;
import com.quanyan.place.entity.apireq.ReqPlaceOrderDetailVo;
import com.quanyan.place.service.ApiPlaceInsideService;
import com.quanyan.place.service.ApiPlaceService;
import com.quanyan.place.wrapper.PlaceOrderServiceWrapper;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Map;


/**
 * Created by Kingson.chan on 2016/8/17 0005.
 * 内部服务调用，不需要对参数进行签名验证，例如提供c++调用
 */
@RestController
@RequestMapping("/api/place/c")
public class ApiPlaceInsideController {

    @Autowired
    ApiPlaceService apiPlaceService;

    @Autowired
    PlaceOrderServiceWrapper placeOrderServiceWrapper;

    @Autowired
    BizPlaceClient bizPlaceClient;

    @Autowired
    ApiPlaceInsideService apiPlaceInsideService;

    @Autowired
    PlaceClient placeClient;


    /**
     * 查看场馆基本信息
     * @param map
     */
    @RequestMapping(value = "/placeBaseInfo")
    public APIResponse<?> placeBaseInfo(@RequestBody @Valid Map<String,Object> map){
        Integer placeId = MapUtils.getInteger(map,"placeId");
        return apiPlaceService.placeBaseInfo(placeId);
    }

    /**
     * 场馆订单状态处理
     * @param reqOrderOperate
     * @param bindingResult
     * @return
     */
    @RequestMapping(value = "/orderOperate")
    public APIResponse<?> orderOperate(@RequestBody @Valid ReqOrderOperate reqOrderOperate,BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        reqOrderOperate.setIsCallBackOrder(PlaceConstants.YES);
        return placeOrderServiceWrapper.orderOperate(reqOrderOperate);
    }

    /**
     * 订单详情
     * @return
     */
    @RequestMapping(value = "/shops/orderDetail")
    @ResponseBody
    public APIResponse placeOrderDetail(@RequestBody @Valid ReqPlaceOrderDetailVo reqPlaceOrderDetailVo){
        return bizPlaceClient.placeOrderDetail(reqPlaceOrderDetailVo);
    }


    /**
     * 获取订单号、流水号接口
     * @param map
     * @return
     */
    @RequestMapping(value = "/orderNoAndNo")
    @ResponseBody
    public APIResponse orderNoAndNo(@RequestBody @Valid Map<String,Object> map){
        return apiPlaceInsideService.getOrderNoAndNo(map);
    }

    /**
     * 获取卡号
     * @param map
     * @return
     */
    @RequestMapping(value = "/cardNo")
    @ResponseBody
    public APIResponse cardNo(@RequestBody @Valid Map<String,Object> map){
        String cardId = apiPlaceInsideService.getCardNo(map);
        if( cardId != null){
            return APIResponse.returnSuccess(cardId);
        }
        return APIResponse.returnFail("");
    }

    /**
     * 达人使用订单接口
     * @param map
     * @return
     */
    @RequestMapping(value = "/orderUse")
    @ResponseBody
    public APIResponse orderUse(@RequestBody @Valid Map<String,Object> map){
        Integer uid = MapUtils.getInteger(map, "uid");
        String placeOrderId = MapUtils.getString(map, "placeOrderId");
        Integer type = MapUtils.getInteger(map, "type");
        return  placeClient.isOrderUse(uid,placeOrderId,type);
    }

}
